﻿using System.Text;

namespace TextManipulation
{
    public static class CyrillicTranslator
    {
        public static bool SmartCompare(this string str1, string str2)
        {
            str1 = str1.Replace(" ", "").ToLower().Translator();
            str2 = str2.Replace(" ", "").ToLower().Translator();
            if (str1.Contains(str2))
            {
                return true;
            }

            str1 = str1.GenerateDoubleMetaphone();
            str2 = str2.GenerateDoubleMetaphone();
            int distance = str1.DamerauLevenshteinDistanceTo(str2);
            if (distance == 1) // one transformation difference is good enough
            {
                return true;
            }

            return false;
        }

        public static string Translator(this string s)
        {
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < s.Length; i++)
            {
                switch (s[i])
                {
                    // not real translations
                    case ('а'): result.Append("а"); break;
                    case ('б'): result.Append("b"); break;
                    case ('в'): result.Append("v"); break;
                    case ('г'): result.Append("g"); break;
                    case ('д'): result.Append("d"); break;
                    case ('е'): result.Append("e"); break;
                    case ('ж'): result.Append("j"); break;
                    case ('з'): result.Append("z"); break;
                    case ('и'): result.Append("i"); break;
                    case ('й'): result.Append("i"); break;
                    case ('к'): result.Append("k"); break;
                    case ('л'): result.Append("l"); break;
                    case ('м'): result.Append("m"); break;
                    case ('н'): result.Append("n"); break;
                    case ('о'): result.Append("o"); break;
                    case ('п'): result.Append("p"); break;
                    case ('р'): result.Append("r"); break;
                    case ('с'): result.Append("s"); break;
                    case ('т'): result.Append("t"); break;
                    case ('у'): result.Append("u"); break;
                    case ('ф'): result.Append("f"); break;
                    case ('х'): result.Append("h"); break;
                    case ('ц'): result.Append("ts"); break;
                    case ('ч'): result.Append("ch"); break;
                    case ('ш'): result.Append("sh"); break;
                    case ('щ'): result.Append("sht"); break;
                    case ('ъ'): result.Append("a"); break;
                    case ('ь'): result.Append("y"); break;
                    case ('ю'): result.Append("yu"); break;
                    case ('я'): result.Append("ya"); break;
                    default: result.Append(s[i].ToString()); break;
                }
            }
            return result.ToString().FixerForStupidPPL();
        }

        private static string FixerForStupidPPL(this string s)
        {
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < s.Length; i++)
            {
                switch (s[i])
                {
                    case ('6'): result.Append("sht"); break;
                    case ('1'): result.Append("a"); break;
                    case ('4'): result.Append("ch"); break;
                    default: result.Append(s[i].ToString()); break;
                }
            }
            return result.ToString();
        }
    }
}